package com.lineage.server.timecontroller.pet;

import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldPet;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PetHprTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(PetHprTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  private static int _time = 0;
  
  public void start() {
    _time = 0;
    int timeMillis = 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    try {
      _time++;
      Collection<L1PetInstance> allPet = WorldPet.get().all();
      if (allPet.isEmpty())
        return; 
      Iterator<L1PetInstance> iter = allPet.iterator();
      while (iter.hasNext()) {
        L1PetInstance pet = iter.next();
        if (HprPet.hpUpdate((L1NpcInstance)pet, _time))
          Thread.sleep(5L); 
      } 
    } catch (Exception e) {
      _log.error("Pet HP自然回复时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      PetHprTimer petHprTimer = new PetHprTimer();
      petHprTimer.start();
    } 
  }
}
